package com.itheima.dao;

import com.github.pagehelper.Page;
import com.itheima.pojo.Address;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.Map;

public interface AddressDao {

    /*
     * 根据条件分页查询
     * */
    @Select("<script> select * from t_address"+
            "<where>"+
            "<if test='value!=null and value.length>0'> and name like  CONCAT('%',#{value},'%')</if>"+
            "</where>"+
            "</script>"
    )
    Page<Address> findByCondition(String queryString);

    /*
     * 新增
     * */
    @Insert("insert into t_address values(#{id},#{name},#{longitude},#{latitude},#{img})")
    void add(Address address);

    /*
     * 根据id查询address对象回显
     * */
    @Select("select * from t_address where id = #{id}")
    Address findByid(Integer id);

    /*
     * 修改体检机构
     * */
    @Update("<script> update t_address"+
            "<set>"+
            "<if test='name!=null'> name=#{name}, </if>"+
            "<if test='longitude!=null'> longitude=#{longitude}, </if>"+
            "<if test='latitude!=null'> latitude=#{latitude}, </if>"+
            "<if test='img!=null'> img=#{img}, </if>"+
            "</set>"+
            "where id = #{id}"+
            "</script>"
    )
    void update(Address address);

    /*
     * 查询中间表，是否有关联数据，有的话不删除
     * */
    @Select("select count(*) from t_order_address where address_id=#{id}")
    int findByAddressId(int id);

    /*
     * 根据id删除体检机构
     * */
    @Delete("delete from t_address where id = #{id}")
    void delete(int id);

    /*
    * 根据OrderId，查询体检机构
    * */
    @Select("select a.name addressName,a.id aid from t_order_address oa, t_address a where oa.address_id=a.id and oa.order_id=#{id}")
    Map findByOrderId(Integer id);
}
